package com.lm.liaoming.tool.choosebirthday;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;


import com.lm.liaoming.R;

import java.util.Calendar;
import java.util.List;

/**
 * 韦敏敏
 */

public class RegionalChoice extends PopupWindow {
    private View mView;
    private TextView finish;
    private Context context;
    private int currentYear;
    private List<String> cityNumber;
    private List<List<String>> areaNumber;
    private LoopView cityview;
    private LoopView areaview;
    private String city;
    private String area;
    private int anInt=0;
    public RegionalChoice(Activity context, List<String> cityNumber,List<List<String>> areaNumber, View.OnClickListener clickListener) {
        super(context);
        this.context = context;
        this.cityNumber=cityNumber;
        this.areaNumber=areaNumber;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.choose_reginal_dialog, null);
        finish = mView.findViewById(R.id.user_info_select_finish);
        /*市*/
        cityview = mView.findViewById(R.id.city);
        /*区*/
        areaview = mView.findViewById(R.id.area);
        initLoopView();
        // 设置按钮监听
        finish.setOnClickListener(clickListener);
        //设置PopupWindow的View
        this.setContentView(mView);
        //设置PopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置PopupWindow弹出窗体的高
        this.setHeight(context.getResources().getDimensionPixelOffset(R.dimen.user_info_dialog_height));
        //设置PopupWindow弹出窗体可点击
        this.setFocusable(true);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xeef6f6f6);
//        设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setAnimationStyle(R.style.livePopStyle);
        Calendar c = Calendar.getInstance();
        currentYear = c.get(Calendar.YEAR);
    }

    private void initLoopView() {
        city = cityNumber.get(0);
        area = areaNumber.get(0).get(1);
        cityview.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected( int index) {
                cityview.invalidate();
                areaview.invalidate();
                city=cityNumber.get(index);
                anInt=index;
                areaview.setItems(areaNumber.get(index));
            }
        });
        areaview.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int indexs) {
                area = areaNumber.get(anInt).get(indexs);
                cityview.invalidate();
                areaview.invalidate();
            }
        });
        cityview.setItems(cityNumber);
        cityview.setInitPosition(0);
        cityview.setTextSize(18);
        cityview.setNotLoop();
        areaview.setInitPosition(0);
        areaview.setTextSize(18);
        areaview.setNotLoop();
        areaview.setItems(areaNumber.get(0));
        areaNumber.get(0).remove(0);
    }

    public String getData() {
        return city + " " + area;
    }

}
